<template>
  <el-dialog title="新增部门" width="40%" :visible="showDialog" @close="close">
    <el-form ref="addDep" :model="depform" :rules="rules" size="mini" style="width:80%;margin: 0 auto;">
      <el-form-item label="部门名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="depform.name" autocomplete="off" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" :label-width="formLabelWidth" prop="code">
        <el-input v-model="depform.code" autocomplete="off" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" :label-width="formLabelWidth" prop="managerId">
        <el-select v-model="depform.managerId" placeholder="请选择负责人" style="width:100%">
          <el-option v-for="item in depManagerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" :label-width="formLabelWidth" prop="introduce">
        <el-input v-model="depform.introduce" type="textarea" :rows="4" placeholder="1-100个字符" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12" style="display:flex;justify-content: center">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="resetForm">取 消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDepartmentList, getDepManagerList, postDepartment } from '@/api/company'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    pid: {
      type: String,
      default: ''
    }
  },
  data() {
    var checkNameRepeat = async(rule, value, callback) => {
      const data = await getDepartmentList()
      const findRes = data.find(item => {
        // console.log('item', item, value)
        return value === item.name
      })
      if (findRes) {
        callback(new Error('部门名称和已有部门不重复'))
      } else {
        callback()
      }
    }
    var checkCodeRepeat = async(rule, value, callback) => {
      const data = await getDepartmentList()
      const findRes = data.find(item => {
        // console.log('item', item, value)
        return value === item.code
      })
      if (findRes) {
        callback(new Error('部门编码和已有部门不重复'))
      } else {
        callback()
      }
    }
    return {
      depform: {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
      },
      rules: {
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur'
          },
          {
            validator: checkCodeRepeat, trigger: 'blur'
          }
        ], // 部门编码
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }, {
          min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur'
        }], // 部门介绍
        managerId: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }], // 部门负责人id
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur'
          },
          {
            validator: checkNameRepeat, trigger: 'blur'
          }
        ] // 部门名称
        // pid: '' // 父级部门的id 不需要做校验
      },
      depManagerList: [],
      formLabelWidth: '100px'
    }
  },
  computed: {},
  created() {
    this.getDepManagerList()
  },
  mounted() {

  },
  methods: {
    async getDepManagerList() {
      const res = await getDepManagerList()
      // console.log('res', res)
      this.depManagerList = res
    },
    close() {
      this.$emit('update:showDialog', false)
    },
    submitForm() {
      this.$refs.addDep.validate(async(valid) => {
        if (valid) {
          // alert('submit!')
          await postDepartment(...this.depform, this.pid)
          console.log('222', 222)
          this.$message.success(`新增部门成功`)
          // 成功之后，要重新调用一下获取部门数据，更新
          this.$emit('updateDep')
          this.close()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.addDep.resetFields()
      this.close()
    }
  }
}
</script>
<style lang='less' scoped>

</style>
